import { message } from "antd";
import apis from "./pushListApi";

export default {
  namespace: "pushList",
  state: {
    list: [],
    totalCount: 0,
    totalPage: 0,
    showModal: false,
    filter: {
      pushPlanName: null,
      pageIndex: 1,
      pageSize: 10,
      pushDesc: null,
      createTime: null,
      status: null,
      order: null // "asc"升序  desc:降序
    }, // 搜索过滤器
    DropDownList: {
      app: [],
      pushRule: [],
      pushScene: [],
      template: []
    }
  },
  effects: {
    // 推送配置查询
    *pushConfQuery({ payload = {} }, { select, call, put }) {
      // 得到当前搜索过滤器的值
      const filter = yield select(state => state.pushList.filter);
      // 当有payload参数不为空时，更新搜索过滤器的值
      const newFilter = { ...filter, ...payload };
      yield put({ type: "update/filter", payload: newFilter });
      // 用最新的搜索过滤器发送Http请求
      const { data, totalCount, totalPage } = yield call(
        apis.pushConfQuery,
        newFilter
      );
      yield put({
        type: "save/data",
        payload: { data, totalCount, totalPage }
      });
    },
    // 推送配置新增
    *pushConfAdd({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushConfAdd, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushConfQuery", payload: {} });
        message.success(errMsg);
        yield put({ type: "trigger/showModal", payload: false });
      }
    },
    // 推送配置删除
    *pushConfDel({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushConfDel, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushConfQuery", payload: {} });
        message.success(errMsg);
      }
    },
    // 推送配置修改
    *pushConfMod({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushConfMod, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushConfQuery", payload: {} });
        message.success(errMsg);
        yield put({ type: "trigger/showModal", payload: false });
      }
    },
    // 推送配置状态修改
    *pushConfStatusMod({ payload = {} }, { call, put }) {
      const res = yield call(apis.pushConfStatusMod, payload);
      const { errCode, errMsg } = res;
      if (errCode === 0) {
        yield put({ type: "pushConfQuery", payload: {} });
        message.success(errMsg);
      }
    },
    // 推送配置新增模块五个下拉框
    *getDropDownList({ payload = {} }, { select, call, put }) {
      const res = yield call(apis.getDropDownList, payload);
      const { data } = res;
      // 得到当前下拉框的值
      const DropDownList = yield select(state => state.pushList.DropDownList);
      yield put({
        type: "save/DropDownList",
        payload: { ...DropDownList, [payload.element]: data }
      });
    },
    // 新增或编辑打开Modal时，请求下拉框的数据
    *showModal(empty, { put }) {
      yield put({
        type: "getDropDownList",
        payload: { element: "app" }
      });
      yield put({
        type: "getDropDownList",
        payload: { element: "pushRule" }
      });
      yield put({
        type: "getDropDownList",
        payload: { element: "pushScene" }
      });
      yield put({
        type: "getDropDownList",
        payload: { element: "template" }
      });
      yield put({ type: "trigger/showModal", payload: true });
    }
  },
  reducers: {
    "save/data": (state, { payload = {} }) => ({
      ...state,
      list: payload.data || [],
      totalCount: payload.totalCount || 0,
      totalPage: payload.totalPage || 0
    }),
    "update/filter": (state, { payload = {} }) => ({
      ...state,
      filter: payload
    }),
    "trigger/showModal": (state, { payload }) => ({
      ...state,
      showModal: payload
    }),
    "save/DropDownList": (state, { payload = {} }) => ({
      ...state,
      DropDownList: payload
    })
  }
};
